using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Runtime.CompilerServices;
using System.Web;
using TuyennvLibrary.SqlClient;
using TuyennvLibrary.ServerApi.Sms;
namespace TuyennvLibrary.ServerApi.Models
{
    public class PartnerSMS
    {
        public int id { get; set; }
        public string name { get; set; }
        public string name_unsign { get; set; }
        public string serviceid { get; set; }
        public DateTime createddate { get; set; }
        public string param_commandcode { get; set; }
        public string param_serviceid { get; set; }
        public string param_message { get; set; }
        public string param_userid { get; set; }
        public string ip_access { get; set; }
        public int status { get; set; }
        public string username { get; set; }
        public string password { get; set; }
        public string class_proccess_script { get; set; }

        public static string COLUMN_ID = "id";
        public static string COLUMN_NAME = "name";
        public static string COLUMN_NAME_UNSIGN = "name_unsign";
        public static string COLUMN_SERVICEID = "serviceid";
        public static string COLUMN_CREATEDDATE = "createddate";
        public static string COLUMN_PARAM_COMMANDCODE = "param_commandcode";
        public static string COLUMN_PARAM_SERVICEID = "param_serviceid";
        public static string COLUMN_PARAM_MESSAGE = "param_message";
        public static string COLUMN_PARAM_USERID = "param_userid";
        public static string COLUMN_IP_ACCESS = "ip_access";
        public static string COLUMN_STATUS = "status";
        public static string COLUMN_USERNAME = "username";
        public static string COLUMN_PASSWORD = "password";


        public static string TableName = "PartnerSMS";

        public PartnerSMS()
        {
            this.id = 0;
            this.name = string.Empty;
            this.name_unsign = string.Empty;
            this.serviceid = string.Empty;
            this.createddate = DateTime.Now;
            this.param_commandcode = string.Empty;
            this.param_serviceid = string.Empty;
            this.param_message = string.Empty;
            this.param_userid = string.Empty;
            this.ip_access = string.Empty;
            this.status = 0;
            this.username = string.Empty;
            this.password = string.Empty;
            this.class_proccess_script = "";
        }
        public bool CheckAccess(string username,string tokenkey, string requestid)
        {
            if (username != this.username)
                return false;
            string server_token = Util.ApiUtilities.md5(this.password + requestid);
            return server_token == tokenkey;
        }
        public bool CheckAccess(string username, string password)
        {
            if (!string.IsNullOrEmpty(this.username)&& username != this.username)
                return false;
            if (!string.IsNullOrEmpty(this.password) && password != this.password)
                return false;
            return true;
        }
        public bool CheckIp(string ip)
        {
            if (string.IsNullOrEmpty(this.ip_access))
                return true;
            foreach (string item in ip_access.Split(','))
            {
                if (item == ip)
                    return true;
            }
            return false;
        }
        
    }
    public class PartnerSMSModel : BaseModel<PartnerSMS>
    {
        public PartnerSMSModel()
        {
            table = PartnerSMS.TableName;
        }
        [MethodImpl(MethodImplOptions.Synchronized)]
        public bool Insert(PartnerSMS obj)
        {
            try
            {
                DB.Insert(table, new SqlParameter[] {
					new SqlParameter("name",obj.name),
					new SqlParameter("name_unsign",obj.name_unsign),
					new SqlParameter("serviceid",obj.serviceid),
					new SqlParameter("createddate",obj.createddate),
					new SqlParameter("param_commandcode",obj.param_commandcode),
					new SqlParameter("param_serviceid",obj.param_serviceid),
					new SqlParameter("param_message",obj.param_message),
					new SqlParameter("param_userid",obj.param_userid),
					new SqlParameter("ip_access",obj.ip_access),
					new SqlParameter("status",obj.status),
					new SqlParameter("username",obj.username),
					new SqlParameter("password",obj.password),
                    new SqlParameter("class_proccess_script",obj.class_proccess_script),
                });
                return true;


            }
            catch (Exception)
            {

                throw;
            }
        }
        [MethodImpl(MethodImplOptions.Synchronized)]
        public bool Update(PartnerSMS obj)
        {
            try
            {
                DB.Update(table, new SqlParameter[] {
					new SqlParameter("name",obj.name),
					new SqlParameter("name_unsign",obj.name_unsign),
					new SqlParameter("serviceid",obj.serviceid),
					new SqlParameter("createddate",obj.createddate),
					new SqlParameter("param_commandcode",obj.param_commandcode),
					new SqlParameter("param_serviceid",obj.param_serviceid),
					new SqlParameter("param_message",obj.param_message),
					new SqlParameter("param_userid",obj.param_userid),
					new SqlParameter("ip_access",obj.ip_access),
					new SqlParameter("status",obj.status),
					new SqlParameter("username",obj.username),
					new SqlParameter("password",obj.password),
                    new SqlParameter("class_proccess_script",obj.class_proccess_script),
                }, new SqlParameter[]{
					new SqlParameter("id",obj.id),
																																																												 
                });
                return true;


            }
            catch (Exception)
            {

                throw;
            }
        }
        [MethodImpl(MethodImplOptions.Synchronized)]
        public bool Delete(int id)
        {
            try
            {
                DB.Delete(table, new SqlParameter[] { 
					new SqlParameter("id",id),
																																																												 
                });
                return true;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public bool DeleteMulti(string ListID)
        {
            try
            {
                if (ListID != string.Empty)
                {
                    foreach (string ID in ListID.Split(','))
                    {
                        Delete(int.Parse(ID));
                    }
                }
                return true;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public List<PartnerSMS> GetByPage(int page, int pagesize, List<PartnerSMS> source)
        {
            List<PartnerSMS> list = new List<PartnerSMS>();
            int index = (page - 1) * pagesize;
            int count = source.Count;
            if (pagesize > count)
                list = source;
            else if (pagesize > count - index)
                list = source.GetRange(index, count - index);
            else
                list = source.GetRange(index, pagesize);
            return list;
        }
        public List<PartnerSMS> GetListBy(DataTable dt)
        {
            List<PartnerSMS> list = new List<PartnerSMS>();
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    PartnerSMS obj = GetBy(dr);
                    list.Add(obj);
                }
            }
            return list;
        }
        public DataTable GetListAll()
        {
            try
            {
                DataTable dt = DB.SelectDataTable(table);
                return dt;
            }
            catch (Exception)
            {

                throw;
            }
        }
        public PartnerSMS GetBy(DataRow dr)
        {
            try
            {
                PartnerSMS obj = new PartnerSMS();
                obj.id = (dr["id"] is DBNull) ? 0 : int.Parse(dr["id"].ToString());
                obj.name = (dr["name"] is DBNull) ? string.Empty : dr["name"].ToString();
                obj.name_unsign = (dr["name_unsign"] is DBNull) ? string.Empty : dr["name_unsign"].ToString();
                obj.serviceid = (dr["serviceid"] is DBNull) ? string.Empty : dr["serviceid"].ToString();
                obj.createddate = (dr["createddate"] is DBNull) ? DateTime.Now : DateTime.Parse(dr["createddate"].ToString());
                obj.param_commandcode = (dr["param_commandcode"] is DBNull) ? string.Empty : dr["param_commandcode"].ToString();
                obj.param_serviceid = (dr["param_serviceid"] is DBNull) ? string.Empty : dr["param_serviceid"].ToString();
                obj.param_message = (dr["param_message"] is DBNull) ? string.Empty : dr["param_message"].ToString();
                obj.param_userid = (dr["param_userid"] is DBNull) ? string.Empty : dr["param_userid"].ToString();
                obj.ip_access = (dr["ip_access"] is DBNull) ? string.Empty : dr["ip_access"].ToString();
                obj.status = (dr["status"] is DBNull) ? 0 : int.Parse(dr["status"].ToString());
                obj.username = (dr["username"] is DBNull) ? string.Empty : dr["username"].ToString();
                obj.password = (dr["password"] is DBNull) ? string.Empty : dr["password"].ToString();
                obj.class_proccess_script = (dr["class_proccess_script"] is DBNull) ? string.Empty : dr["class_proccess_script"].ToString();
                return obj;
            }
            catch (Exception)
            {
            }

            return new PartnerSMS();
        }
        public PartnerSMS GetBy(DataTable dt)
        {
            if (dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    return GetBy(dr);
                }
            }
            return new PartnerSMS();
        }
        public PartnerSMS GetByID(int id)
        {
            try
            {
                DataTable dt = DB.SelectDataTable(null, table, new SqlParameter[] { 
					new SqlParameter("id",id),
																																																												
                }, null);
                return GetBy(dt);
            }
            catch (Exception)
            {

                throw;
            }
        }
        public DataTable GetListAll(string orderby)
        {
            try
            {
                DataTable dt = DB.SelectDataTable(null, table, orderby);
                return dt;
            }
            catch (Exception)
            {

                throw;
            }
        }
        private bool check(string InServiceId, string ServiceId)
        {
            if (string.IsNullOrEmpty(InServiceId))
                return true;
            string[] arrInServiceId = InServiceId.Trim().Split(',');
            foreach (string item in arrInServiceId)
            {
                if (item == ServiceId)
                    return true;
            }
            return false;
            
        }
        public List<string> GetListServiceId(string InServiceId, string OrderBy)
        {
            List<string> listResult = new List<string>();
            List<PartnerSMS> list = GetListBy(GetListAll(OrderBy));
            foreach (PartnerSMS item in list)
            {
                string[] arrServiceId = item.serviceid.Trim().Split(',');
                foreach (string serviceId in arrServiceId)
                {
                    if (!listResult.Contains(serviceId)&& check(InServiceId, serviceId))
                    {
                        listResult.Add(serviceId);
                    }
                }
            }
            return listResult;
        }
        public DataTable GetServiceId(string InServiceId,string OrderBy)
        {
            string query = "select * from " + table + " where 1=1";
            List<PartnerSMS> list = GetListBy(GetListAll(OrderBy));
           

            if (!string.IsNullOrEmpty(InServiceId))
            {
                string[] arrServiceid = InServiceId.Split(',');
                
                foreach (string serviceid in arrServiceid)
                {
                    InServiceId = InServiceId.Replace(serviceid, string.Format("'{0}'",serviceid));
                }
                query += string.Format(" and serviceid in ({0})", InServiceId);
            }
            query += " order by " + OrderBy;
            return DB.ExecuteDataTable(query, null);
        }
        public DataTable Search(string name, string serviceid, int status)
        {

            string query = string.Format("select * from " + table + " where 1=1");
            if (!string.IsNullOrEmpty(name))
            {
                query += string.Format(" and (name like '%{0}%' or name_unsign like '%{1}%')", name, name);

            }
            if (!string.IsNullOrEmpty(serviceid))
            {
                query += string.Format(" and serviceid like '%{0}%'", serviceid);
            }
            if (status != 2)
            {
                query += string.Format(" and status={0}", status);
            }
            query += " order by createddate desc";
            DataTable dt = DB.ExecuteDataTable(query, null);
            return dt;
        }
    }
}
